time series analysis

packages
Author

Tony Duan

Published

August 30, 2023

The basic steps in a forecasting task: Step 1: Problem definition Step 2: Gathering information Step 3: Preliminary (exploratory) analysis Step 4: Choosing and fitting models Step 5: Using and evaluating a forecasting model

Code
library(tidyverse)
library(tidymodels)
library(tsibble)
library(tsibbledata)
library(lubridate)
library(dplyr)
library(fpp3)

1 data as tsibble

convert data into tsibble

Code
weather <- nycflights13::weather %>% 
  select(origin, time_hour, temp, humid, precip)
weather_tsbl <- as_tsibble(weather, key = origin, index = time_hour)
Code
head(weather_tsbl)
# A tsibble: 6 x 5 [1h] <America/New_York>
# Key:       origin [1]
  origin time_hour            temp humid precip
  <chr>  <dttm>              <dbl> <dbl>  <dbl>
1 EWR    2013-01-01 01:00:00  39.0  59.4      0
2 EWR    2013-01-01 02:00:00  39.0  61.6      0
3 EWR    2013-01-01 03:00:00  39.0  64.4      0
4 EWR    2013-01-01 04:00:00  39.9  62.2      0
5 EWR    2013-01-01 05:00:00  39.0  64.4      0
6 EWR    2013-01-01 06:00:00  37.9  67.2      0

find index from tsibble

Code
index(weather_tsbl)
time_hour

find key from tsibble

Code
key(weather_tsbl)
[[1]]
origin

2 plot time series data

Code
melsyd_economy <- ansett  %>% 
  filter(Airports == "MEL-SYD", Class == "Economy")  %>% 
  mutate(Passengers = Passengers/1000)
Code
glimpse(melsyd_economy)
Rows: 282
Columns: 4
Key: Airports, Class [1]
$ Week       <week> 1987 W26, 1987 W27, 1987 W28, 1987 W29, 1987 W30, 1987 W31…
$ Airports   <chr> "MEL-SYD", "MEL-SYD", "MEL-SYD", "MEL-SYD", "MEL-SYD", "MEL…
$ Class      <chr> "Economy", "Economy", "Economy", "Economy", "Economy", "Eco…
$ Passengers <dbl> 20.167, 20.161, 19.993, 20.986, 20.497, 20.770, 21.111, 20.…
Code
autoplot(melsyd_economy, Passengers) +
  labs(title = "Ansett airlines economy class",
       subtitle = "Melbourne-Sydney",
       y = "Passengers ('000)")

Code
PBS |>
  filter(ATC2 == "A10") |>
  select(Month, Concession, Type, Cost) |>
  summarise(TotalC = sum(Cost)) |>
  mutate(Cost = TotalC / 1e6) -> a10
Code
autoplot(a10, Cost) +
  labs(y = "$ (millions)",
       title = "Australian antidiabetic drug sales")

Code
glimpse(vic_elec)
Rows: 52,608
Columns: 5
$ Time        <dttm> 2012-01-01 00:00:00, 2012-01-01 00:30:00, 2012-01-01 01:0…
$ Demand      <dbl> 4382.825, 4263.366, 4048.966, 3877.563, 4036.230, 3865.597…
$ Temperature <dbl> 21.40, 21.05, 20.70, 20.55, 20.40, 20.25, 20.10, 19.60, 19…
$ Date        <date> 2012-01-01, 2012-01-01, 2012-01-01, 2012-01-01, 2012-01-0…
$ Holiday     <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE…

seasonal month plot:

Code
vic_elec |> gg_season(Demand, period = "year") +
  theme(legend.position = "none") +
  labs(y="MWh", title="Electricity demand: Victoria")

seasonal week plot:

Code
vic_elec |> gg_season(Demand, period = "month") +
  theme(legend.position = "none") +
  labs(y="MWh", title="Electricity demand: Victoria")

seasonal day plot:

Code
vic_elec |> gg_season(Demand, period = "week") +
  theme(legend.position = "none") +
  labs(y="MWh", title="Electricity demand: Victoria")

seasonal time plot:

Code
vic_elec |> gg_season(Demand, period = "day") +
  theme(legend.position = "none") +
  labs(y="MWh", title="Electricity demand: Victoria")

subseries plot:

Code
a10 %>% 
  gg_subseries(Cost) +
  labs(
    y = "$ (millions)",
    title = "Australian antidiabetic drug sales"
  )

3 Reference

https://fable.tidyverts.org/

https://otexts.com/fpp3/index.html